// pages/communicate_list/communicate_list.js
const app = getApp(),
  chatIM = require('../../utils/websocket.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo: null,
    sessionList: [],
    sessionListMore: {
      page: 1,
      pagesize: 50,
      text: '加载中...'
    },
  },
  //刷新
  refresh() {
	this.setData({
		sessionListMore: {
			page: 1,
			pagesize: 50,
			text: '加载中...'
		  },
	  })
    this.sessionList();
  },
  //聊天列表
  sessionList() {
    let sessionListMore = this.data.sessionListMore;
    chatIM.wsSend({
      type: 'sessionlist',
      page: sessionListMore.page,
      pagesize: sessionListMore.pagesize
    });
    wx.stopPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    let page = this;
    let userinfo = app.getUserinfo();
    this.setData({
      userinfo: userinfo
    })
    this.refresh();
    //获取列表成功回调
    app.eventBus.on("session_list_callback", function (data) {
      let curPageData = data.data;
      page.setData({
        //sessionList: page.data.sessionList.concat(curPageData),
        sessionList: curPageData,
        sessionListMore: {
          page: 1,
          pagesize: 50,
          text: ''
        },
      })
	})
	app.eventBus.on(app.Events.UserLogin, this.eventOnUserLogin, this);
    app.eventBus.on(app.Events.UserLogout, this.eventOnUserLogout, this);
	app.eventBus.on(app.Events.UserinfoChange, this.eventOnUserinfoChange, this);
	//未读消息
	if (app.globalData.chat_config.SocketTask) {
		chatIM.setMessageBadge(false)
	 }
    app.eventBus.on(app.Events.WebsocketMessageCount, this.eventWebsocketMessageCount, this);
    //刷新列表
    app.eventBus.on('refresh_session', function () {
      page.refresh();
	})
  },

  //前往聊天室
  bindChat(event) {
	let index = event.currentTarget.dataset.index;
	let item = this.data.sessionList[index];
	let receive_user_id = this.data.userinfo.id != item.receive_user_id ? item.receiveuser.id : item.senduser.id;
	let to_username = this.data.userinfo.id != item.receive_user_id ? item.receiveuser.nickname : item.senduser.nickname;
	let chat = {
		company_id: item.company_id,
		resume_id: item.resume_id,
		receive_user_id: receive_user_id,
		to_username: to_username
	}
	chat.refresh_status = item.unread_num>0?1:0;
	wx.navigateTo({
		url: '/pages/communicate/index',
		events:{
			refreshSession:data=>{
				chatIM.setMessageBadge(false)
			}
		},
		success(res) {
			res.eventChannel.emit('chatTransfor', chat)
		}
	})
},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
	app.eventBus.off(app.Events.UserLogin, this.eventOnUserLogin);
	app.eventBus.off(app.Events.UserLogout, this.eventOnUserLogout);
	app.eventBus.off(app.Events.UserinfoChange, this.eventOnUserinfoUpdate);
    app.eventBus.off(app.Events.WebsocketMessageCount, this.eventWebsocketMessageCount);
    app.eventBus.off('session_list_callback');
    app.eventBus.off('refresh_session');
  },
  eventOnUserLogin(userinfo) {
    //this.refresh();
  },
  eventOnUserLogout() {
    //this.refresh();
  },
  eventOnUserinfoChange: function(){
	this.refresh();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.refresh();
  },
  //未读消息
  eventWebsocketMessageCount(data) {
    var list = this.getTabBar().data.list;
    list[1].megNum = data;
    this.getTabBar().setData({
      list
    })
  },
  onShow: function () {
    var that = this;
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      });
    }
  },
})